import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * http请求转发
 */
public class S15_RequestForwardingServlet01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据用户来确定该用户是什么身份

        String username = req.getParameter("username");
        //如果是同一个request对象(请求转发)，可以在不同的servlet中，使用getParameter取出参数
        if ("tom".equals(username)) {
            //分配
            req.setAttribute("role", "管理员");
        } else {
            req.setAttribute("role", "普通用户");
        }

        //获取分发器
        //参数 /managerServlet 是要转发的servlet的url-pattern
        // / 会被解析成 http://localhost:8080/servlet
        RequestDispatcher requestDispatcher =
                req.getRequestDispatcher("/managerServlet");
        //表示把当前的servlet的request对象和response对象传递给下一个servlet对象
        requestDispatcher.forward(req, resp);

    }
}
